package com.shotgun.my.fgc.startup.zfn;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.core.io.ClassPathResource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/***
 * *---------------------------
 * @Description: zfn启动类
 * ***---------------------------
 * @author: wulingming
 * @date: 2024年05月23日 12:01
 * @version: v1.0
 * ****---------------------------
 */
public class StartUpZfnAndDrawIo {


    /**
     * @Description: The entry point of application.
     * @param args the input arguments
     * @throws Exception the exception
     */
    public static void main(String[] args) throws Exception {

        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            //触发模板生成
            try {
                execute();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });


        CompletableFuture<Void> drawioFuture = CompletableFuture.runAsync(() -> {
            //触发生成Drawio模板生成
            try {
                StartUpDrawio.execute();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });


        //等待结束
        future.join();
        drawioFuture.join();

    }

    public static void execute() throws Exception{
        List<String> warnings = new ArrayList<>();

        long curtime = System.currentTimeMillis();
        ClassPathResource generatorConfigResource = new ClassPathResource("config/zfn/generatorConfig.xml");
        //		File configFile = new File("generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(generatorConfigResource.getInputStream());
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

        long curtime2 = System.currentTimeMillis();

        System.out.println(String.join("\n", warnings));
        System.out.println("总耗时(ms)： " + (curtime2 - curtime));
        System.out.println(StartUpZfnAndDrawIo.class.getSimpleName() + "........................end.");
    }
}
